package com.lebang.controller.sys;

import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.domain.AppAdContro;
import com.lebang.entity.TaskRewards;
import com.lebang.example.TaskRewardsExample;
import com.lebang.model.Whether;
import com.lebang.service.MemberNewTaskService;
import com.lebang.service.sys.AppLookCategoryService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: hwq
 * @Date: 2019/7/25 15:22
 * @Version 1.0
 */
@Controller
@RequestMapping("/sys/newtask")
public class MemberNewTaskController {
    @Autowired
    private MemberNewTaskService taskService;
    @Autowired
    private AppLookCategoryService appLookCategoryService;
    @Login
    @GetMapping("/list")
    public String list() {
        return "/newhand/newapi-newhand";
    }
    @Login
    @PostMapping("/data")
    @ResponseBody
    public Map data(HttpServletRequest request, Integer page, Integer limit) {
        Map map = new HashMap(1);
        TaskRewardsExample example = new TaskRewardsExample();
        example.setOrderByClause("has_deleted ");
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        example.createCriteria().andParentIdNotEqualTo(0);
        List<TaskRewards> rewardsList = taskService.selectByExample(example);
        Long count = taskService.countByExample(example);
        map.put("count", count);
        map.put("code", 0);
        map.put("data", rewardsList);
        return map;
    }
    @Login
    @GetMapping("/edit")
    public String list(TaskRewards rewards,Model model) {
        if (rewards.getId() != null) {
            rewards = taskService.selectById(rewards.getId());
        } else {
            rewards.setHasDeleted(Whether.NO.getDbIndex());
        }
        model.addAttribute("item", rewards);
        model.addAttribute("looklist", appLookCategoryService.getAll());
        return "/newhand/newapi-newhand-edit";
    }
    @Login
    @PostMapping("/enable")
    @ResponseBody
    @ConfigRecord
    public JsonData enable(@RequestBody TaskRewards rewards) {
        taskService.updateReworads(rewards);
        return Callback.success("success", "操作成功");
    }
    @Login
    @PostMapping("/edit")
    @ResponseBody
    @ConfigRecord
    public JsonData edit(TaskRewards rewards,HttpServletRequest request) {
        int i=0;
        if(rewards.getId()==null){
             i = taskService.insert(rewards);
        }else {
            i = taskService.updateReworads(rewards);
        }
        if (i > 0) {
            return Callback.success("success", "操作成功", null);
        } else {
            return Callback.fail("fail", "操作失败", null);
        }

    }
}
